package com.huluobo.higt4demo1911.utils.gift.display;

import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.huluobo.higt4demo1911.utils.gift.GiftBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by lc on 2022/5/6.
 * 核心业务代码
 */
public class GiftController implements IGiftController {

    private List<GiftBean> showList = new ArrayList<>();
    private List<GiftBean> loadingList = new ArrayList<>();
    private boolean isRun;

    private int maxCount;

    public GiftController(int maxCount) {
        this.maxCount = maxCount;
    }

    @Override
    public List<GiftBean> getShowingList() {
        return showList;
    }

    @Override
    public List<GiftBean> getLoadingList() {
        return loadingList;
    }

    @Override
    public void addGift(GiftBean giftBean) {
        if (isShow()) {
            showList.add(giftBean);
            start();
        } else {
            loadingList.add(giftBean);
        }
    }

    @Override
    public void start() {
        //启动线程
        if (isRun) {
            return;
        }
        isRun = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                //循环数据,如果两个list中只要有值就会进入循环,没有则会跳出
                while (loadingList.size() > 0 || showList.size() > 0) {
                    //有等待的数据需要进行展示
                    if (loadingList.size() > 0) {
                        //有无展示的位置 有的话将其填满
                        //计算有多少空位置
                        int addCount = 0;
                        if (maxCount - showList.size() > loadingList.size()) {
                            addCount = loadingList.size();//添加的数量等于loading的数量
                        } else {
                            addCount = maxCount - showList.size();//添加的数量等于showList剩余的数量
                        }
                        for (int i = 0; i < addCount; i++) {
                            GiftBean bean = loadingList.get(0);
                            showList.add(bean);
                            loadingList.remove(0);
                        }
                    }

                    //展示队列处理完成,对所有展示时间数据进行排序,0位置为时间最早要销毁的对象
                    Collections.sort(showList);
                    LogUtils.json(showList);
                    //TODO 通过状态 和时间开控制数据回调
                    for (GiftBean bean : showList) {
                        if (bean.status == 0) {

                        }
                    }
                }
                isRun = false;
            }
        }).start();
    }

    @Override
    public boolean isShow() {
        return showList.size() < maxCount;
    }

    @Override
    public void destroy() {

    }

    private OnGiftChanger onGiftChanger;

    public void setOnGiftChanger(OnGiftChanger onGiftChanger) {
        this.onGiftChanger = onGiftChanger;
    }

    public interface OnGiftChanger {
        void show(GiftBean giftBean);

        void finish(GiftBean giftBean);
    }
}
